package com.example.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.result.Result;
import com.example.common.result.YiFeiException;
import com.example.entity.hz.Messageconfiguration;
import com.example.entity.hz.Messagetype;
import com.example.entity.hz.Printmodule;
import com.example.entity.hz.Printtemplate;
import com.example.mapper.MessageconfigurationMapper;
import com.example.service.MessagetypeService;
import com.example.mapper.MessagetypeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 14501
* @description 针对表【messageType】的数据库操作Service实现
* @createDate 2024-09-06 09:03:07
*/
@Service
public class MessagetypeServiceImpl extends ServiceImpl<MessagetypeMapper, Messagetype>
    implements MessagetypeService{

    @Resource
    private MessagetypeMapper messagetypeMapper;
    @Resource
    private MessageconfigurationMapper messageconfigurationMapper;

    @Override
    @Transactional
    public Result insertMessageType(List<Messagetype> messagetypeList) {
        try{
            if (!messagetypeList.isEmpty()){
                messagetypeList.forEach(arr -> {
                    arr.setCreateTime(DateUtil.format(new Date(), "yyyyMMdd"));
                    arr.setStatus("开启");
                    this.messagetypeMapper.insert(arr);
                });
            }
            return Result.success();
        }catch (Exception e){
            throw new YiFeiException(500, "消息模块新增发生异常"+e.getMessage());
        }
    }

    @Override
    public Result queryMessageTypePage(Integer current, Integer size) {
        try{
            Map<String,Object> map = new HashMap<>();
            PageHelper.startPage(current,size);
            List<Messagetype> messagetypeList = this.messagetypeMapper.selectList(null);
            PageInfo<Messagetype> messagetypePageInfo = new PageInfo<>(messagetypeList);
            map.put("pages", messagetypePageInfo.getPages());
            map.put("total", messagetypePageInfo.getTotal());
            map.put("list", messagetypePageInfo.getList());
            return Result.success(map);
        }catch (Exception e){
            throw new YiFeiException(500,"查询消息模块分页发生异常"+e.getMessage());
        }
    }

    @Override
    public Result deleteMessageType(List<Integer> uuidList) {
        try {
            if (!uuidList.isEmpty()){
                uuidList.forEach(arr -> {
                    int i = this.messagetypeMapper.deleteById(arr);
                    if (i>0){
                        this.messageconfigurationMapper.delete(new LambdaQueryWrapper<Messageconfiguration>().eq(Messageconfiguration::getUuid, arr));
                    }
                });
                return Result.success();
            }
            return Result.error("请输入正确参数");
        }catch (Exception e){
            throw new YiFeiException(500, "消息通知模块删除发生异常"+e.getMessage());
        }
    }

    @Override
    public Result queryMessageConfigurationList(Integer uuid) {
        try {
            List<Messageconfiguration> messageconfigurationList = this.messageconfigurationMapper.selectList(new LambdaQueryWrapper<Messageconfiguration>().eq(Messageconfiguration::getUuid, uuid));
            return Result.success(messageconfigurationList);
        }catch (Exception e){
            throw new YiFeiException(500, "通过uuid查询消息通知模版发生异常"+e.getMessage());
        }
    }

    @Override
    public Result<List<Messagetype>> queryMessageTypeList() {
        try {
            List<Messagetype> messagetypeList = this.messagetypeMapper.selectList(null);
            if (!messagetypeList.isEmpty()){
                messagetypeList.forEach(arr -> {
                    List<Messageconfiguration> messageconfigurationList = this.messageconfigurationMapper.selectList(new LambdaQueryWrapper<Messageconfiguration>().eq(Messageconfiguration::getUuid, arr.getUuid()));
                    arr.setMessageconfigurationList(messageconfigurationList);
                });
            }
            return Result.success(messagetypeList);
        }catch (Exception e){
            throw new YiFeiException(500, "通过uuid查询消息通知模版发生异常"+e.getMessage());
        }
    }

    @Override
    public Result<Messagetype> queryOneMessageType(Integer uuid) {
        try {
            Messagetype messagetype = this.messagetypeMapper.selectById(uuid);
            return Result.success(messagetype);
        }catch (Exception e){
            throw new YiFeiException(500, "通过uuid查询消息通知模块详细信息发生异常"+e.getMessage());
        }
    }

    @Override
    public Result updateMessageType(Messagetype messagetype) {
        try{
            messagetype.setUpdateTime(DateUtil.format(new Date(), "yyyyMMdd"));
            int update = this.messagetypeMapper.updateById(messagetype);
            if (update>0){
                return Result.success();
            }
            return Result.error("修改失败!请检验传参uuid");
        }catch (Exception e){
            throw new YiFeiException(500,"消息通知模块修改发生异常"+e.getMessage());
        }
    }
}




